package org.htw.osgi.interceptor.bgfiller;

import java.awt.*;
import org.htw.osgi.renderservice.essentials.*;
import javax.servlet.ServletRequest;

/**
 * <p>Zeichnet den Hintergrund anhand der übergebenen Farbwerte:</p>
 * <ul>
 * 	 <li>bgr : rot   (0-255) default: 0 </li>
 *   <li>bgg : gelb  (0-255) default: 0 </li>
 *   <li>bgb : blau  (0-255) default: 0 </li>
 *   <li>bga : alpha (0-255) default: 255 (deckend)</li>
 * </ul>
 * 
 * @author  Benjamin Friedrich (<a href="mailto:benjamin_friedrich@gmx.de">mailto:benjamin_friedrich@gmx.de</a>)
 * @version 1.0  Juni 2009
 */
public final class Activator 
extends            ARenderActivator
{
	private final static int    PRIORITY     = 100;
	private final static int    DEF_BGR      = 0;
	private final static int    DEF_BGG      = 0;
	private final static int    DEF_BGB      = 0;
	private final static int    DEF_BGA      = 255;
	private final static int    DEF_WIDTH    = 100;
	private final static int    DEF_HEIGHT   = 100;
	private final static String PARAM_WIDTH  = "width";
	private final static String PARAM_HEIGHT = "height";
	private final static String PARAM_BGR    = "bgr";
	private final static String PARAM_BGG    = "bgg";
	private final static String PARAM_BGB    = "bgb";
	private final static String PARAM_BGA    = "bga";
	private final static String SERVICE_NAME = "org.htw.osgi.interceptor.bgfiller";
	
	/**
	 * Konstruktor von Activator des Interceptors
	 * org.htw.osgi.interceptor.bgfiller
	 */
	public Activator()
	{
		super(PRIORITY);
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.htw.osgi.renderservice.essentials.IInterceptor#processRequest(javax.servlet.ServletRequest)
	 */
	@Override
	public void processRequest(ServletRequest request) 
	{
		if(request instanceof IRenderServiceContext)
		{
			final IRenderServiceContext   context  = (IRenderServiceContext) request;
			final Graphics2D              g        = context.getGraphics();
			final String                  width    = context.getParameter(PARAM_WIDTH);
			final String                  height   = context.getParameter(PARAM_HEIGHT);
			final Color                   bakColor = g.getColor();
			
			final String bgr = context.getParameter(PARAM_BGR);
			final String bgg = context.getParameter(PARAM_BGG);
			final String bgb = context.getParameter(PARAM_BGB);
			final String bga = context.getParameter(PARAM_BGA);
			
			final Color bgColor = new Color(
					bgr != null ? Integer.parseInt(bgr) : DEF_BGR,
				    bgg != null ? Integer.parseInt(bgg) : DEF_BGG,
					bgb != null ? Integer.parseInt(bgb) : DEF_BGB,
					bga != null ? Integer.parseInt(bga) : DEF_BGA
											);
			
			final Integer intWidth  = (width  != null ? Integer.parseInt(width) : DEF_WIDTH);
			final Integer intHeight = (height != null ? Integer.parseInt(height): DEF_HEIGHT);
			
			g.setColor(bgColor);

			final Paint         bakPaint  = g.getPaint();
			final GradientPaint gradPaint = new GradientPaint( 0.0f,
														       0.0f,
														       bgColor,
														       intWidth,
														       intHeight,
														       Color.WHITE
															 );
			g.setPaint(gradPaint);
			g.fillRect(0, 
					   0, 
					   intWidth, 
					   intHeight);	
						
			g.setPaint(bakPaint);
			g.setColor(bakColor);
		}
	}

	/*
	 * (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString()
	{
		return SERVICE_NAME;
	}
}